﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using ECCIT;
using ECCIT.Data;
using OgilvyOne.Sharan.Model;
using OgilvyOne.Sharan.IDAL;

namespace OgilvyOne.Sharan.DAL
{
    public class WeiboLogDAL : IDALWeiboLog
    {
        private const string C_TABLE_NAME = "tbl_WeiboLog";
        private const string C_COL_LOG_ID = "col_LogID";
        private const string C_COL_REQ_USER_ID = "col_ReqUserID";
        private const string C_COL_REQ_WEIBO_USER_ID = "col_ReqWeiboUserID";
        private const string C_COL_REQ_TIME = "col_ReqTime";
        private const string C_COL_REQ_STATE = "col_ReqState";
        private const string C_COL_REQ_RESULT = "col_ReqResult";

        private static readonly string S_SQL_INSERT_LOG = RetrieveInserLogSql();
        private static readonly string S_SQL_GET_LATEST_LOG_BY_USER_ID = RetrieveGetLatestLogByUserIDSql();
        private static readonly string S_SQL_GET_LATEST_LOG_BY_WEIBO_USER_ID = RetrieveGetLatestLogByWeiboUserIDSql();

        private static string RetrieveInserLogSql()
        {
            return string.Format("insert into {0} ({1},{2},{3},{4},{5}) values(@{1}, @{2}, @{3}, @{4}, @{5});"
            , C_TABLE_NAME, C_COL_REQ_USER_ID, C_COL_REQ_WEIBO_USER_ID, C_COL_REQ_TIME, C_COL_REQ_STATE, C_COL_REQ_RESULT);
        }

        private static string RetrieveGetLatestLogByUserIDSql()
        {
            return string.Format("select top 1 * from {0} where {1}=@{1} and {2}=1 order by {3} desc", C_TABLE_NAME, C_COL_REQ_USER_ID, C_COL_REQ_STATE, C_COL_REQ_TIME);
        }

        private static string RetrieveGetLatestLogByWeiboUserIDSql()
        {
            return string.Format("select top 1 * from {0} where {1}=@{1} and {2}=1 order by {3} desc", C_TABLE_NAME, C_COL_REQ_WEIBO_USER_ID, C_COL_REQ_STATE, C_COL_REQ_TIME);
        }
        #region IDALWeiboLog 成员

        public ResultInfo Insert(WeiboLogModel log)
        {
            ResultInfo result = new ResultInfo(false);
            if (log == null)
                return result;

            try
            {
                IDataParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@" + C_COL_REQ_USER_ID, SqlDbType.Int),
                    new SqlParameter("@" + C_COL_REQ_WEIBO_USER_ID, SqlDbType.BigInt),
                    new SqlParameter("@" + C_COL_REQ_TIME, SqlDbType.DateTime),
                    new SqlParameter("@" + C_COL_REQ_STATE, SqlDbType.Int),
                    new SqlParameter("@" + C_COL_REQ_RESULT, SqlDbType.NVarChar, 4000)
                };

                if (log.RequestUserID.HasValue)
                    parms[0].Value = log.RequestUserID.Value;
                else
                    parms[0].Value = DBNull.Value;

                if (log.RequestWeiboUserID.HasValue)
                    parms[1].Value = log.RequestWeiboUserID.Value;
                else
                    parms[1].Value = DBNull.Value;

                parms[2].Value = log.RequestTime;
                parms[3].Value = log.RequestState;
                parms[4].Value = log.RequestResult;

                int rowsAffected = DBHelper.ExecuteNonQuery(S_SQL_INSERT_LOG, parms);
                if (rowsAffected > 0)
                    result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = "Error occurred in WeiboLogDAL.Insert(). Message: " + ex.Message;
            }

            return result;
        }

        public ResultInfo GetLatestLogByUserID(int userID)
        {
            ResultInfo result = new ResultInfo(false);

            try
            {
                IDataParameter[] parms = new SqlParameter[] { new SqlParameter("@" + C_COL_REQ_USER_ID, SqlDbType.Int) };
                parms[0].Value = userID;

                using (IDataReader reader = DBHelper.ExecuteReader(S_SQL_GET_LATEST_LOG_BY_USER_ID, parms))
                {
                    while (reader.Read())
                    {
                        WeiboLogModel model = new WeiboLogModel();
                        model.ID = (int)reader[C_COL_LOG_ID];
                        model.RequestUserID = (int)reader[C_COL_REQ_USER_ID];
                        model.RequestWeiboUserID = (reader[C_COL_REQ_WEIBO_USER_ID] == null || reader[C_COL_REQ_WEIBO_USER_ID] == DBNull.Value) ? null : (long?)reader[C_COL_REQ_WEIBO_USER_ID];
                        model.RequestTime = (DateTime)reader[C_COL_REQ_TIME];
                        model.RequestState = (int)reader[C_COL_REQ_STATE];
                        model.RequestResult = (string)reader[C_COL_REQ_RESULT];

                        result.ReturnObject = model;
                    }

                    result.Result = true;
                }
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = "Error occurred in WeiboLogDAL.GetLatestLogByUserID(). Message: " + ex.Message;
                result.Exception = ex;
            }

            return result;
        }

        public ResultInfo GetLatestLogByWeiboUserID(long weiboUserID)
        {
            ResultInfo result = new ResultInfo(false);

            try
            {
                IDataParameter[] parms = new SqlParameter[] { new SqlParameter("@" + C_COL_REQ_WEIBO_USER_ID, SqlDbType.BigInt) };
                parms[0].Value = weiboUserID;

                using (IDataReader reader = DBHelper.ExecuteReader(S_SQL_GET_LATEST_LOG_BY_WEIBO_USER_ID, parms))
                {
                    while (reader.Read())
                    {
                        WeiboLogModel model = new WeiboLogModel();
                        model.ID = (int)reader[C_COL_LOG_ID];
                        model.RequestUserID = (reader[C_COL_REQ_USER_ID] == null || reader[C_COL_REQ_USER_ID] == DBNull.Value) ? null : (int?)reader[C_COL_REQ_USER_ID];
                        model.RequestWeiboUserID = (reader[C_COL_REQ_WEIBO_USER_ID] == null || reader[C_COL_REQ_WEIBO_USER_ID] == DBNull.Value) ? null : (long?)reader[C_COL_REQ_WEIBO_USER_ID];
                        model.RequestTime = (DateTime)reader[C_COL_REQ_TIME];
                        model.RequestState = (int)reader[C_COL_REQ_STATE];
                        model.RequestResult = (string)reader[C_COL_REQ_RESULT];

                        result.Result = true;
                        result.ReturnObject = model;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = "Error occurred in WeiboLogDAL.GetLatestLogByWeiboUserID(). Message: " + ex.Message;
            }

            return result;
        }

        #endregion
    }
}